package com.zhiqiang.nativeplayer.frame;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.Objects;

/**
 * @author paul
 * @version 1.0
 * @date 17:53 2024/5/4
 * start frame
 */
public class MainFrame {
    
    /* default component */
    private static JFrame jFrame;
    
    /** port */
    private static JTextField port_Field = null;
    /** address */
    private static JComboBox<String> jcb = null;
    
    /**start form, using for select web address and port*/
    public void startFrame(){
    
        jFrame = new JFrame("NativePlayer");
        JRootPane jRootPane = jFrame.getRootPane();
        Container contentPane = jRootPane.getContentPane();
        
        //main panel
        JPanel mainPanel = new JPanel();
        mainPanel.setBorder(new EmptyBorder(10,10,10,10));
        contentPane.add(mainPanel);
        mainPanel.setLayout(new GridLayout(2,1));
        
        /* net and port gird */
        JPanel jPanel_for_net = new JPanel();
        mainPanel.add(jPanel_for_net);
        jPanel_for_net.setLayout(new GridLayout(3,1));
        
        
        //address label
        jPanel_for_net.add(new JLabel("select an address:"));
        //address box
        jcb = createAddressBox();
        jPanel_for_net.add(jcb);
    
        
        /* port panel */
        JPanel jPanel_for_port = new JPanel();
        jPanel_for_port.setBorder(new EmptyBorder(5,0,5,0));
        jPanel_for_port.setLayout(new GridLayout(1,2));
        jPanel_for_net.add(jPanel_for_port);
        //port label
        jPanel_for_port.add(new JLabel("select a port:"));
        //port textArea
        port_Field = new JTextField("8881");
        jPanel_for_port.add(port_Field);
        
        
        //start button
        JPanel jPanel_for_startButton = new JPanel();
        mainPanel.add(jPanel_for_startButton);
        JButton button_startApplication = new JButton("Start Application");
        button_startApplication.addActionListener(new StartListener());
        jPanel_for_startButton.add(button_startApplication);
        
        //init JFrame
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }
    
    
    private static NetWorkParts netWorkParts = new NetWorkParts();
    
    /** create net box*/
    private JComboBox<String> createAddressBox(){
        return netWorkParts.createAddressBox();
    }
    
    static String getPort(){
        return port_Field.getText();
    }
    
    static AddressPo getAddress(){
        String selected = ((String) Objects.requireNonNull(jcb.getSelectedItem())).split(" ")[0];
        return netWorkParts.getAddressPo(selected);
    }
    
    static JFrame getjFrame(){
        return jFrame;
    }
    
}

